package com.chikitbd.shadowsocks.service;

import com.chikitbd.common.Crawler;
import com.chikitbd.common.PropertiesLoader;
import com.chikitbd.common.Util;
import com.chikitbd.shadowsocks.entity.ss.Configs;
import com.chikitbd.shadowsocks.entity.ss.ShadowsocksConfig;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CrawlerShadowsocksImpl implements Crawler {

	private Gson gson = new GsonBuilder().setPrettyPrinting().create();

    public static void main(String[] args) {
        CrawlerShadowsocksImpl crawler_Shadowsocks = new CrawlerShadowsocksImpl();
        try {
            crawler_Shadowsocks.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void start() throws IOException {
        String originalGUIConfig = "";
        HtmlPage page;
        try (WebClient webClient = new WebClient(BrowserVersion.CHROME);) {
            page = webClient.getPage(PropertiesLoader.SHADOWSOCKS_URL);
            originalGUIConfig = this.readConfig(PropertiesLoader.SHADOWSOCKS_PATH);
            this.writeConfig(originalGUIConfig, page, PropertiesLoader.SHADOWSOCKS_PATH);
        }
    }

    @Override
    public String readConfig(String filePath) throws IOException {
        return Util.readFile(filePath);
    }

    @Override
    public boolean writeConfig(String originalConfig, HtmlPage page, String filePath) throws IOException {
		ShadowsocksConfig shadowsocksConfig = gson.fromJson(originalConfig.toString(), ShadowsocksConfig.class); // 读取原来的配置文件, 通过Gson转化为JavaBean
		shadowsocksConfig.setConfigs(getShadowsocksConfigs(page));
		String crawlerJsonResult = gson.toJson(shadowsocksConfig);
		boolean result = Util.writeFile(crawlerJsonResult, filePath);
		if (result) {
			Util.printfSuccess("SS-iShadowsocks");
		}
		return result;
    }

	/**
	 * @param page
	 * @return List<Configs>
	 * @throws
	 * @Title: getShadowsocksConfigs
	 * @Description: 通过Element的ID爬取相关的内容
	 * @Author: LanceBD
	 * @Date: 2018年9月12日
	 */
	private List<Configs> getShadowsocksConfigs(HtmlPage page) {

		List<Configs> crawlerSever = new ArrayList<Configs>();

		for (int i = 0; i < ServerInfo.serverInfos.size(); i++) {
			String[] strServerInfo = Util.getServerElement(ServerInfo.serverInfos.get(i));
			Configs config = new Configs();
			try {
				HtmlElement ip_div = page.getHtmlElementById(strServerInfo[0]);
				HtmlElement password_div = page.getHtmlElementById(strServerInfo[1]);
				HtmlElement port_div = page.getHtmlElementById(strServerInfo[2]);

				config.setServer(ip_div.asText());
				config.setPassword(password_div.asText());
				config.setServer_port(Integer.parseInt(port_div.asText()));
				config.setTimeout(5);
				config.setMethod("aes-256-cfb");
				crawlerSever.add(config);

//			System.out.println("ip_div: " + ip_div.asText());
//			System.out.println("port_div: " + port_div.asText());
//			System.out.println("password_div: " + password_div.asText());
//			System.out.println(password_div.asXml());
//				System.out.println(crawlerSever);
			} catch (Exception e) {

			}
		}
		return crawlerSever;
	}
}
